package com.example.erhuo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.erhuo.LikeInformation;
import com.example.erhuo.R;

import java.util.LinkedList;

public class SoldAdapter extends BaseAdapter {
    private LinkedList<LikeInformation> mData;
    private Context mContext;

    public SoldAdapter(LinkedList<LikeInformation> mData, Context mContext) {
        this.mContext = mContext;
        this.mData = mData;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_bought, parent, false);
        }
        TextView contactSolder = convertView.findViewById(R.id.item_bought_contact);
        TextView title = convertView.findViewById(R.id.item_bought_title);
        TextView price = convertView.findViewById(R.id.item_bought_price);
        TextView solderId = convertView.findViewById(R.id.item_bought_solder);
        TextView status = convertView.findViewById(R.id.item_bought_status);
        ImageView image = convertView.findViewById(R.id.item_bought_image);
        LikeInformation like = mData.get(position);
        if (like.getTitle() == null || like.getTitle().equals("")) {
            title.setText("商品标题");
        } else {
            title.setText(like.getTitle());
        }
        if (like.getImage() != null) {
            image.setImageBitmap(like.getImage());
        }
        if (like.getContactSolder() == null || like.getContactSolder().equals("")) {
            contactSolder.setText("买家没有添写联系方式");
        } else {
            contactSolder.setText("买家联系方式:" + like.getContactSolder());
        }
        //todo
        price.setText("￥" + like.getPrice());
        solderId.setText("买方:" + like.getSolderId());
        status.setText(like.getOrderStatus());
        switch (like.getOrderStatus()) {
            case "0":
                status.setText("代报价");
                break;
            case "1":
                status.setText("已报价，等待卖家接受");
                break;
            case "2":
                status.setText("买家已接受");
                break;
        }
        return convertView;
    }

    public void addLike(LikeInformation like) {
        mData.add(like);
    }

    public void removeAll() {
        mData.clear();
    }
}
